package azku.photoviews;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import azku.entitys.EGroupType;

public class XNavListView extends ListView implements OnItemClickListener {

	private List<Map<String, Object>> mData;
	private int mIType = 0;

	public XNavListView(Context context) {
		super(context);
		Init();
	}

	public XNavListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Init();
	}

	public XNavListView(Context context, int iType) {
		super(context);
		mIType = iType;
		Init();
	}

	private void Init() {
		mData = this.getData();
		XNavListViewAdapter adapter = new XNavListViewAdapter(this.getContext());
		this.setAdapter(adapter);
		this.setBackgroundColor(Color.BLACK);
		this.setPadding(0, 10, 0, 0);
		this.setDividerHeight(1);
		this.setOnItemClickListener(this);
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (EGroupType item : DataManager.It().GetGroupTypeList()) {
			if (item.getIType() != mIType)
				continue;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(EGroupType.KEY_TypeName, item.getTypeName());
			map.put(EGroupType.KEY_TypeID, item.getTypeID());
			list.add(map);
		}
		return list;
	}

	public final class XViewHolder {
		public ImageView img;
		public TextView title;
	}

	public class XNavListViewAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public XNavListViewAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return mData.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			XViewHolder holder;
			if (convertView == null) {
				holder = new XViewHolder();
				convertView = mInflater.inflate(R.layout.navlistviewitem, null);

				holder.title = (TextView) convertView
						.findViewById(R.id.navlist_text);
				holder.img = (ImageView) convertView
						.findViewById(R.id.navlist_img);
				convertView.setTag(holder);
			} else {
				holder = (XViewHolder) convertView.getTag();
			}

			holder.title.setText((String) mData.get(position).get(
					EGroupType.KEY_TypeName));
			holder.title.setTag(mData.get(position).get(EGroupType.KEY_TypeID));
			return convertView;
		}
	}

	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
		// TODO Auto-generated method stub
		XViewHolder holder = (XViewHolder) v.getTag();
		if (holder != null) {
			Intent intent = new Intent(this.getContext(),
					GroupListActivity.class);
			intent.putExtra(EGroupType.KEY_TypeID,
					Integer.parseInt(holder.title.getTag().toString()));
			intent.putExtra(EGroupType.KEY_TypeName, holder.title.getText()
					.toString());
			this.getContext().startActivity(intent);
		}
	}
}
